<?php

/*
  Aurus CMS - free OOP CMS
  Copyright (C) 2010  Nestor Yanchuk

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
if (!defined('ROOT'))
    die();

include('functions.php');

class Template implements Core {

    public $template = '';
    public $tagsInTemplates = array();
    public $regExpTagsInTemplates = array();
    private $find_array = array();
    private $currentTemplate = '';
    private $templateToLoad = '';
    private $pregSet = array();

    /**
     * Return an instance of class
     *
     * @return Template
     */
    public static function getInstance() {
	return Factory::get(__CLASS__);
    }

    function getCurrent() {
	return $this->currentTemplate;
    }

    function load($tpl) {
	$this->currentTemplate = $tpl;
	$this->templateToLoad = Aurus::getInstance()->getTemplateToLoad();
	$this->template = '';
    }

    function setTemplate($tplContent) {
	$this->template = $tplContent;

	if (!$this->template) {
	    error('Template not found!<br /><strong>/templates/' . $this->templateToLoad . '/' . $tpl . '.tpl</strong>');
	}

	if (strpos($this->template, '[template]') !== false) {
	    $this->template = preg_replace("`\[template\](.+?)\[\/temlate\]`ise", "\$this->_include('\\1')", $this->template);
	}
    }

    function set($find, $replace = '', $template = false, $callback = false) {
	if (false === $template) {
	    $template = $this->currentTemplate;
	}
	if ($callback) {
	    $this->find_array[$template][$find] = array($callback, true);
	} else {
	    $this->find_array[$template][$find] = array($replace, false);
	}
    }

    public function setArrayTag($tag, $arrayData) {
	foreach ($arrayData as $key => $value) {
	    $this->set('{'.$tag.'['.$key.']}', $value);
	}
    }

    function deleteBlock($b) {
	$this->pregSet[] = array("`\[{$b}\](.*?)\[\/{$b}\]`ies", '');
    }

    function pregSet($pattern, $replacement) {
	$this->pregSet[] = array($pattern, $replacement);
    }

    function out() {
	if ($this->template == '') {
	    if (is_file(ROOT . '/templates/' . $this->templateToLoad . '/' . $this->currentTemplate . '.php')) {
		ob_start();
		include ROOT . '/templates/' . $this->templateToLoad . '/' . $this->currentTemplate . '.php';
		$this->setTemplate(filter(ob_get_clean(), 'set_template_' . $this->currentTemplate));
	    } else {
		$this->setTemplate(filter(file_get_contents(ROOT . '/templates/' . $this->templateToLoad . '/' . $this->currentTemplate . '.tpl'), 'set_template_' . $this->currentTemplate));
	    }
	}
	$template = $this->template;

	foreach ($this->pregSet as $b)
	    $template = preg_replace($b[0], $b[1], $template);

	if (isset($this->regExpTagsInTemplates[$this->currentTemplate]) && count($this->regExpTagsInTemplates[$this->currentTemplate])) {
	    foreach ($this->regExpTagsInTemplates[$this->currentTemplate] as $find => $replace) {
		$template = preg_replace($find, $replace, $template);
	    }
	}
	foreach ($this->find_array[$this->currentTemplate] as $find => $repl) {
	    if ($repl[1]) {
		while ($fpos = strpos($template, $find)) {
		    $findlen = strlen($find);
		    $template = substr_replace($template, call_user_func($repl[0]), $fpos, $findlen);
		}
	    } else {
		$repl[0] = filter($repl[0], $this->currentTemplate . '>' . trim($find, '{}[]/'));
		$template = str_replace($find, $repl[0], $template);
	    }
	}

	return $template;
    }

    function _include($data) {
	return file_get_contents(ROOT . '/templates/' . $this->templateToLoad . '/' . $data . '.tpl');
    }

    function getTag($tag) {
	$tag = trim($tag, '{}');
	$tag = '{' . $tag . '}';

	return isset($this->find_array[$this->currentTemplate][$tag]) ? $this->find_array[$this->currentTemplate][$tag][0] : '';
    }

}